<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      方法1:使用Set去重
    */
      /* function uniqueArr(arr) {
        return [...new Set(arr)];
      }

      const arr = [1, 2, 1, 3, 5, 3, 4, 3, 5, 2, 6, 4];
      const result = uniqueArr(arr);
      console.log(result); */

      /* 
        数组去重的方法2:
          遍历向新数组插入值,通过includes判断
      
      */
      /* function uniqueArr(arr) {
        const resultArr = [];
        arr.forEach((item) => {
          resultArr.includes(item) ? "" : resultArr.push(item);
        });
        return resultArr;
      }

      const arr = [1, 2, 1, 3, 5, 3, 4, 3, 5, 2, 6, 4];
      const result = uniqueArr(arr);
      console.log(result); */

      /* 
        数组去重方法3
          reduce+includes
      */

      function uniqueArr(arr) {
        /* return arr.reduce((prev, current) => {
          prev.includes(current) ? "" : prev.push(current);
          return prev;
        }, []); */

        return arr.reduce(
          (prev, current) =>
            prev.includes(current) ? prev : [...prev, current],
          []
        );
      }

      const arr = [1, 2, 1, 3, 5, 3, 4, 3, 5, 2, 6, 4];
      const result = uniqueArr(arr);
      console.log(result);
    </script>
  </body>
</html>
